/*
Copyright 2009 - 2013 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.compactmvc.services.air.sqlservice
{
	import org.compactmvc.services.air.interfaces.ISQLService;
	import org.compactmvc.interfaces.IModel;
	import org.compactmvc.patterns.Controller;
	import org.compactmvc.patterns.Model;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;

	public class SQLService extends Model implements IModel, ISQLService
	{
		private var _sqlConnection:SQLConnection = new SQLConnection();
		private var _sqlStatement:SQLStatement;
		private var _data:Object;
		private var _databasePath:String;
		private var _resultExecutionType:String;
		
		public function SQLService(name:String, controller:Controller)
		{
			super(name, controller);
		}
		
	
		protected function reset():void
		{		
			_sqlConnection.close();
			_sqlStatement = null;
		}
		
		override public function removeHandler():void
		{
			reset();
		}
		
		private function connect():void
        { 	
        	if(databasePath == null)
        		throw new Error('databasePath not defined!)');      
        	
    		if( !File.applicationDirectory.resolvePath(databasePath).exists)
    		{	
    			throw new Error('DatabaseFile not exist: ' + databasePath);        
    		}		
    		_sqlConnection = new SQLConnection();
		    _sqlConnection.open( new File(databasePath) );	
    		
        }
         		
        protected function execute(executionType:String, sql:String=null, itemClass:Class=null):void
        {			
        	if(!_sqlConnection.connected)
        		connect();
        	
        	_resultExecutionType = executionType;	
        	_sqlStatement = new SQLStatement();
			_sqlStatement.sqlConnection = _sqlConnection;
    		_sqlStatement.text = sql;
        	_sqlStatement.itemClass = itemClass;
        	_sqlStatement.addEventListener(SQLEvent.RESULT, statementResultHandler, false, 0, true);		
			_sqlStatement.addEventListener(SQLErrorEvent.ERROR, statementErrorHandler, false, 0, true);
        	_sqlStatement.execute ();  		
        }
        
        private function statementErrorHandler(e:SQLErrorEvent):void
		{		
			_sqlConnection.close();
			_sqlStatement = null;	
			
			errorHandler(e.text);
			
		}
        
    	protected function statementResultHandler(e:SQLEvent):void
        {
        	_sqlConnection.close();
			_sqlStatement = null;
        		
        	var result:Object = e.target.getResult();
        	if(result.data)
        	{
        		_data = result.data;
        		resultHandler(_resultExecutionType);
        	}   
        }
        
        public function errorHandler(message:String):void 
		{
			//overriden by subclass
        }
        
        public function resultHandler(executionType:String):void 
		{
			//overriden by subclass
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
		}
		
		protected function set databasePath(value:String):void
		{
			_databasePath = value;
		}
		
		protected function get databasePath():String
		{
			return _databasePath;
		}
				
	}
}
